import { http, setToken } from '@/utils'

const loginSetToken = payload => ({ type: 'login/setToken', payload })

const sendCode = mobile => {
  return async () => {
    try {
      await http.get(`/sms/codes/${mobile}`)
    } catch (e) {}
  }
}

// 登录  values传入的用户数据
const login = values => {
  return async dispatch => {
    const res = await http.post('/authorizations', values)
    // 成功则保存token到本地
    if (res.data.message.toLowerCase() === 'ok') {
      const tokens = res.data.data
      // 存储token
      setToken(tokens)
      // token分派到redux中
      dispatch(loginSetToken(tokens))
    }
  }
}
export { login,sendCode }
